package cn.edu.scut.fsearch.relative;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;

import jeasy.analysis.MMAnalyzer;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.nutch.util.NutchConfiguration;


public class IndexUpdate{
	private String basePath = NutchConfiguration.create().get("suggest.dir","../webapps/ROOT/");
	private String BASIC_INDEX_DIR = basePath + File.separatorChar + "query-index";
	private String TEMP_INDEX_DIR = basePath + File.separatorChar + "query-index-temp";
	private String LOG_DIR = basePath + File.separatorChar + "logs" + File.separatorChar;
	private String FIELD_HASHCODE = "hashcode";
	private String FIELD_QUERY = "query";
	private String FIELD_PAGE_NUMBER="page_number";
	private String FIELD_WORD_TIME="word_time";
	private IndexWriter writer;
	
	public static void main(String[] args){
		IndexUpdate li=new IndexUpdate();
		li.updateQueryIndex();
	}
	
	public void updateQueryIndex(){
		try{
			writer = new IndexWriter(TEMP_INDEX_DIR, new MMAnalyzer(), true,IndexWriter.MaxFieldLength.UNLIMITED);
			IndexSearcher is=new IndexSearcher(BASIC_INDEX_DIR);
			HashMap<String,QueryWord> hm=new HashMap<String,QueryWord>();
			Calendar today = Calendar.getInstance();
			today.setTimeInMillis(System.currentTimeMillis()-(1000*60*60*24));
			String day=new SimpleDateFormat("yyyy-MM-dd").format(today.getTime());
			File log=new File(LOG_DIR+"search.log."+day);//log file named by date time
			BufferedReader br=new BufferedReader(new FileReader(log));
			String s=br.readLine();
			while(s!=null){
				int splitLength = s.split(" ").length;
				String query=s.substring(0, s.lastIndexOf(" "));
				int page_num=Integer.parseInt(s.split(" ")[splitLength-1]);
				if (page_num != 0){
					int num=0;
					Query q = new TermQuery(new Term(FIELD_HASHCODE, String.valueOf(query.hashCode())));
					Hits hits = is.search(q);
					if(hits.length()>0){
						for(int i=0;i<hits.length();i++){
							num+=Integer.parseInt(hits.doc(i).get(FIELD_WORD_TIME));
						}
					}
					is.getIndexReader().deleteDocuments(new Term(FIELD_HASHCODE, String.valueOf(query.hashCode())));
					QueryWord qw=hm.get(query);
					if(qw!=null){
						qw.setWord_time(qw.getWord_time()+1+num);
					}
					else{
						qw=new QueryWord();
						qw.setHashcode(query.hashCode());
						qw.setPage_number(page_num);
						qw.setQuery(query);
						qw.setWord_time(1+num);
						hm.put(query, qw);
				}
				}
				s=br.readLine();
			}
			Iterator<String> it=hm.keySet().iterator();
			while(it.hasNext()){
				String key=it.next();
				QueryWord qw=hm.get(key);
				Document doc = new Document();
				doc.add(new Field(FIELD_HASHCODE, String.valueOf(qw.getHashcode()), Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new Field(FIELD_QUERY, qw.getQuery(), Field.Store.YES, Field.Index.ANALYZED));
				doc.add(new Field(FIELD_PAGE_NUMBER,String.valueOf(qw.getPage_number()), Field.Store.YES, Field.Index.NO));
				doc.add(new Field(FIELD_WORD_TIME,String.valueOf(qw.getWord_time()), Field.Store.YES, Field.Index.NOT_ANALYZED));
				writer.addDocument(doc);
			}
			writer.optimize();
			writer.close();
			is.close();
			IndexWriter writer1 = new IndexWriter(BASIC_INDEX_DIR, new MMAnalyzer(), false,IndexWriter.MaxFieldLength.UNLIMITED);
			Directory[] dirs=new Directory[1];
			dirs[0]=FSDirectory.getDirectory(TEMP_INDEX_DIR);
			writer1.addIndexesNoOptimize(dirs);
			writer1.optimize();
			writer1.close();
			br.close();
					
			System.out.println("Index Updated");
		}
		catch(Exception ex){
			ex.printStackTrace();
		}
	}
}